package novel.common.utils.weixin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import novel.common.utils.string.StringUntils;
import novel.common.utils.weixin.message.HttpUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sutong on 2017/11/26.
 */
public class wxAuth {
    /*
 * 获取用户微信授权第二步获取access_token 根据第一步获取到的code去换取access_token
 * 根据实现步骤不同，此方法只获取到用户openId即为结束 注意，此access_token与基础参数access_token不一样
 *
 * 如若，今后开发需要用户信息等，根据微信API去实现剩余步骤
 */
    public static Map<String, String> getAccessToken(String code,String appId,String secret) throws Exception {
        long start = System.currentTimeMillis();
        String url = " https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appId+"&secret="+secret+"&code="+code+"&grant_type=authorization_code ";
        String result = null;
        try {
            result = HttpUtil.sendGet(url, "utf-8", 1000);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("发起网络请求出错");
        }
        if (!result.equals("")) {
            JSONObject json = JSON.parseObject(result);
            if (json.get("access_token") != null) {
                Map<String, String> authData = new HashMap<String, String>();
                authData.put("access_token", json.getString("access_token"));
                authData.put("expires_in", json.getString("expires_in"));
                authData.put("refresh_token", json.getString("refresh_token"));
                authData.put("openid", json.getString("openid"));
                authData.put("scope", json.getString("scope"));
                System.out.println("调用获取网页授权accessToken接口耗时-->" + (System.currentTimeMillis() - start) + "毫秒");
                System.out.println("-------------------我是获取openId方法：" + json.getString("openid"));
                return authData;
            } else {
                System.out.println("调用获取网页授权accessToken接口失败-->" + result);
                throw new Exception(result);
            }
        } else {
            throw new Exception("调用获取网页授权accessToken接口返回数据为空");
        }

    }

    /**
     * 获取openId
     *
     * @return
     */
    public static String getOpenId(String code,String appId,String secret) {
        String openid = "";
        if (StringUntils.isEmpty(code)) {
            System.out.println("code值为空");
        }
        try {
            Map<String, String> authData = getAccessToken(code,appId,secret);
            openid = authData.get("openid");
            System.out.println("-------------------我是获取openId方法：" + authData.get("openid"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("执行到了1");
        return openid;
    }

    public static String getWxUserInfo(String code,String appId,String secret) {
        String userInfo = "";
        if (StringUntils.isEmpty(code)) {
            System.out.println("code值为空");
        }
        try {
            Map<String, String> authData = getAccessToken(code,appId,secret);
            String openid = authData.get("openid");
            String access_token = authData.get("access_token");
            System.out.println("-------------------获取openId方法：" + authData.get("openid"));
            System.out.println("-------------------access_token：" + authData.get("access_token"));
            String url="https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid+"&lang=zh_CN ";
            userInfo = HttpUtil.sendGet(url, "utf-8", 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userInfo;
    }
}
